#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   ：try -> 数值微分
@IDE    ：PyCharm
@Author ：csl_forever
@Date   ：2020/9/13 15:31
@Desc   ：
=================================================='''
import numpy as np
import matplotlib.pyplot as plt


def numerical_diff(f, x):
    h = 1e-4  # 1*10的-4次方
    return (f(x + h) - f(x - h)) / (2 * h)


def funciton_1(x):
    return 0.01 * x ** 2 + 0.1 * x


def tangent_line(f, x):
    d = numerical_diff(f, x)
    y = f(x) - d * x
    return lambda t: d * t + y


x = np.arange(0.0, 20.0, 0.1)
h = funciton_1(x)
fx = tangent_line(funciton_1, 5)
h1 = fx(x)
fx2 = tangent_line(funciton_1, 10)
h2 = fx2(x)
plt.xlabel("x")
plt.ylabel("f(x)")
plt.plot(x, h)
plt.plot(x, h1)
plt.plot(x, h2)
plt.show()
print(numerical_diff(funciton_1, 5))
print(numerical_diff(funciton_1, 10))
